package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.adapter.DiscoverBookAdapter;
import me.shurufa.adapter.DiscoverUserAdapter;
import me.shurufa.model.BookListResponse;
import me.shurufa.model.UserListResponse;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.widget.DiscoverRecyclerView;

/**
 * 发现页面用户列表Fragment
 *
 * @FileName: me.shurufa.fragments.DiscoverUserFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-21 21:40
 */
public class DiscoverUserFragment extends BaseListFragment {

    @Bind(R.id.hotauthor_recyclerview)
    DiscoverRecyclerView userRecyclerView;

    DiscoverUserAdapter userAdapter;
    ArrayList dataList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover_user, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        dataList = new ArrayList();
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        userRecyclerView.setLayoutManager(manager);
        userAdapter = new DiscoverUserAdapter(this, dataList);
        mAdapter = userAdapter;
        userRecyclerView.setAdapter(userAdapter);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        加载数据 true为写入缓存
        loadData(true);
    }

    /**
     * 解析为UserListResponse对象
     *
     * @param data
     * @return
     */
    @Override
    protected BaseResponse parseData(String data) {
        Gson gson = Global.getGson();
        return gson.fromJson(data, UserListResponse.class);
    }

    /**
     * 该列表缓存文件名为UID+discover_book
     * 按用户加载缓存
     *
     * @return
     */
    @Override
    protected String cacheKey() {
        return "discover_user";
    }

    /**
     * 加载方法
     *
     * @param isNew true写入缓存 false不写入缓存
     */
    private void loadData(final boolean isNew) {
        new RequestServerTask<UserListResponse>(UserListResponse.class) {

            @Override
            protected String requestServer() {
                return HttpUtil.get(NetworkConstants.GET_HOT_USERS, null);
            }

            @Override
            protected void onSuccess(UserListResponse result) {
                bindData(result, isNew);
                EventBus.getDefault().post(new Intent(Constants.ACTION_DISCOVER_REFRESH_COMPLETE));
            }
        }.start();
    }


    @Override
    protected void addDataToList(BaseResponse response, boolean isNew) {
        UserListResponse resp = (UserListResponse) response;
        if (isNew) {
            userAdapter.clear();
        }
        userAdapter.append(resp.data);
    }

    @Override
    protected void showEmptyView() {

    }

    @Override
    protected void hideEmptyView() {

    }

    @Override
    protected void setHasMore(boolean hasMore) {

    }

    public void refresh(){
        loadData(true);
    }
}
